package io.qar.dishes.controller;

import cn.hutool.core.lang.Snowflake;
import io.qar.commons.log.annotation.LogOperation;
import io.qar.commons.mybatis.generator.CustomSequenceGenerator;
import io.qar.commons.tools.constant.Constant;
import io.qar.commons.tools.page.PageData;
import io.qar.commons.tools.utils.Result;
import io.qar.commons.tools.utils.ExcelUtils;
import io.qar.commons.tools.validator.AssertUtils;
import io.qar.commons.tools.validator.ValidatorUtils;
import io.qar.commons.tools.validator.group.AddGroup;
import io.qar.commons.tools.validator.group.DefaultGroup;
import io.qar.commons.tools.validator.group.UpdateGroup;
import io.qar.dishes.dto.DishesDTO;
import io.qar.dishes.excel.DishesExcel;
import io.qar.dishes.service.DishesService;
import io.qar.dto.UploadDTO;
import io.qar.feign.OssFeignClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
* 菜品信息表
*
* @author yyyyjinying 1020066395@qq.com
* @since 3.0 2024-06-10
*/
@RestController
@Slf4j
@RequestMapping("dishes")
@Api(tags="菜品信息表")
public class DishesController {
    @Autowired
    private DishesService dishesService;
    @Autowired
    private Snowflake snowflake;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    // @PreAuthorize("hasAuthority('sys:dishes:page')")
    public Result<PageData<DishesDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<DishesDTO> page = dishesService.page(params);

        return new Result<PageData<DishesDTO>>().ok(page);
    }



    @GetMapping("{id}")
    @ApiOperation("信息")
    // @PreAuthorize("hasAuthority('sys:dishes:info')")
    public Result<DishesDTO> get(@PathVariable("id") Long id){
        DishesDTO data = dishesService.get(id);

        return new Result<DishesDTO>().ok(data);
    }
    @GetMapping("destail/{id}")
    @ApiOperation("餐单详情")
    // @PreAuthorize("hasAuthority('sys:dishes:info')")
    public Result<DishesDTO> destail(@PathVariable("id") Long id){
        try {
            DishesDTO dishesDTO = dishesService.detailsByDishId(id);
            return new Result<DishesDTO>().ok(dishesDTO);
        } catch (Exception e) {
            log.error(e.getMessage());
            return new Result<DishesDTO>().error(e.getMessage());
        }
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    // @PreAuthorize("hasAuthority('sys:dishes:save')")
    public Result save(@RequestBody DishesDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        dto.setDishId(snowflake.nextId());
        dishesService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    // @PreAuthorize("hasAuthority('sys:dishes:update')")
    public Result update(@RequestBody DishesDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        dishesService.update(dto);
        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    // @PreAuthorize("hasAuthority('sys:dishes:delete')")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        dishesService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    // @PreAuthorize("hasAuthority('sys:dishes:export')")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<DishesDTO> list = dishesService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, "菜品信息表", list, DishesExcel.class);
    }

}